/*
 * Created on 28/mag/07
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package it.unibo.deis.lia.mmhc.test;

import java.net.*;
import java.io.*;

public class MMHCTestServerThroughput extends Thread {

	private Socket s;

	public static void main(String argv[]) throws Exception {
		ServerSocket server = new ServerSocket(3333);
		while (true) {
			System.out.println("MACTestServerThroughput waiting for...");
			Socket s = server.accept();
			MMHCTestServerThroughput macTest = new MMHCTestServerThroughput(s);
			macTest.start();
		}
	}

	public MMHCTestServerThroughput(Socket s) {
		this.s = s;
	}

	public void run() {
		try {
			System.out.println("MACTestServerThroughput new client!");
			InputStream is = s.getInputStream();

			long time1 = System.currentTimeMillis();
			byte[] buffer = new byte[1024];
			int totalByte = 0;
			int read = is.read(buffer);
			while (read > 0) {
				totalByte = totalByte + read;
				read = is.read(buffer);
			}
			long time2 = System.currentTimeMillis();

			System.out.println("time1 " + time1);
			System.out.println("time2 " + time2);
			System.out.println("(time2-time1) " + (time2 - time1));
			System.out.println("totalByte " + totalByte);

			float throughput = (float) totalByte / (time2 - time1);
			System.out.println("throughput " + throughput + "(byte/ms)");
			System.out.println("throughput " + throughput * 1000 + "(byte/s)");
			System.out.println("throughput " + throughput * 1000 / 1024
					+ "(KB/s)");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
